@if($purchase->status == 1)
    <div class="btn-group" style="margin-left: 5px;">
        <a class="btn btn-sm btn-danger plan-revoke" href="javascript:void(0);" data-id="{{ $id }}"><i class="fa fa-trash"></i> 撤销计划</a>
    </div>
    <div class="btn-group" style="margin-left: 5px;">
        <button class="btn btn-sm btn-primary pur-order" data-id="{{ $id }}"><i class="fa fa-check"></i> 下单</button>
    </div>
@endif
@if($purchase->status == 2)
    <div class="btn-group" style="margin-left: 5px;">
        <button class="btn btn-sm btn-default pur-revoke" data-id="{{ $id }}"><i class="fa fa-check"></i> 撤销下单</button>
    </div>
    <div class="btn-group" style="margin-left: 5px;">
        <button class="btn btn-sm btn-success pur-contract-modal" data-id="{{ $id }}"><i class="fa fa-retweet"></i>&nbsp;生成采购合同</button>
    </div>
    <div class="btn-group" style="margin-left: 5px;">
        <button class="btn btn-sm btn-warning pur-close" data-id="{{ $id }}"><i class="fa fa-close"></i> 关闭</button>
    </div>
@endif
<div class="modal fade" id="purContractModal" tabindex="-1" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" style="width: 90%;">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script language="JavaScript" type="text/javascript">
    $(function () {
        //撤销计划
        $('.plan-revoke').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            swal({
                title: "确定撤销？",
                text : "确定要撤销计划吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('planRevoke',id,resolve)
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success('提交成功 !');
                        window.location.href = '{{ url('xqc/product-pickup/pickup-order') }}';
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //下单
        $('.pur-order').unbind('click').click(function(e) {
            e.preventDefault();
            var commitDate = $('.grid-editable-commit_date').data('value');
            if(!commitDate){
                swal('请选择交期', '', 'error');
                return false;
            }
            var id = $(this).data('id');
            _ajax('purOrder',id);
        });

        //撤销下单
        $('.pur-revoke').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            swal({
                title: "确定撤销？",
                text : "确定要撤销下单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('purRevoke',id,resolve)
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success('提交成功 !');
                        $.pjax.reload("#order-edit-form");
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //关闭
        $('.pur-close').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            swal({
                title: "确定关闭？",
                text : "确定要关闭此单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        _ajaxConfirm('purClose',id,resolve)
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success('提交成功 !');
                        $.pjax.reload("#order-edit-form");
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        var _ajax = function(action,param){
            $.ajax({
                method: 'post',
                url: '/xqc/product-pickup/pickup-order-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    action: action,
                    param: param,
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success('提交成功 !');
                            $.pjax.reload("#order-edit-form");
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        };

        var _ajaxConfirm = function(action,param,resolve){
            $.ajax({
                method: 'post',
                url: '/xqc/product-pickup/pickup-order-ajax',
                data: {
                    _token: "{{ csrf_token() }}",
                    action: action,
                    param: param
                },
                success: function (data) {
                    resolve(data);
                }
            });
        };

        //生成采购合同
        $('.pur-contract-modal').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            $("#purContractModal").modal({
                remote : '/xqc/product-pickup/order-modal?action=purContract&param=' + id
            });
        });
        $("#purContractModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        });
    });
</script>
